package com.blockchainenergy.blockChain;

import java.util.ArrayList;
import java.util.List;

public class KeyChainCluster {
    //     单例模式类
    private static volatile KeyChainCluster keyChain;

    private int nodeNum = 10;

    public int getNodeNum() {
        return nodeNum;
    }

    // 创建单例对象
    public static KeyChainCluster getInstance() {
        if (keyChain == null) {
            synchronized (KeyChainCluster.class) {
                if (keyChain == null) {
                    keyChain = new KeyChainCluster();
                }
            }
        }
        return keyChain;
    }

    private List<KeyChain> chain;

    private KeyChainCluster() {
        chain = new ArrayList<>();
        for (int i = 0; i < nodeNum; i++) {
            chain.add(new KeyChain());
            keyStrings.add(new char[10]);
        }
    }

    private List<char[]> keyStrings;

    public void addBlockEveryKeyNode() {
        for (int i = 0; i < nodeNum; i++) {
            KeyChain keyChain = chain.get(i);
            String s = new String(keyStrings.get(i));
            keyChain.addBlock(s);
        }
    }

    public void sendStringToNode(int node, char c, int i) {
        keyStrings.get(node - 1)[i] = c;
    }

    public char[][] getXKey(int x) {
        char[][] res = new char[nodeNum][];
        for (int i = 0; i < nodeNum; i++) {
            KeyBlock keyBlock = chain.get(i).getChain().get(i);
            res[i] = keyBlock.getKey().toCharArray();
        }
        return res;
    }
}
